package com.company;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 删除list中元素为n的元素
 */
public class RemoveListDemo {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("n");

        for(int i=0;i<list.size();i++){
            String str = list.get(i);
            if(str.equals("n")){
                list.remove(i);
                i--;
            }
        }
        System.out.println(list);
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            String s = iterator.next();
            if(s.equals("n")){
                iterator.remove();
            }
        }
        System.out.println(list);
    }
}
